package com.cxh.controller.sys;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cxh.base.BaseController;
import com.cxh.entity.SysMenu;
import com.cxh.entity.SysRoleMenu;
import com.cxh.entity.SysUser;
import com.cxh.entity.SysUserRole;
import com.cxh.util.CommonUtility;
import com.cxh.util.DateUtility;
import com.cxh.util.json.JSONUtil;

import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 类说明：
 * @author  ChenXH
 * @date  2018-11-29 11:14:13
 */
@Controller
@RequestMapping(value = "/controller/login")
public class LoginController extends BaseController {

    /**
     * 方法说明：进入页面
     * @author  ChenXH
     * @date  2018-11-29 11:14:13
     */
    @RequestMapping(value = "/login")
    public String list (HttpServletRequest request, HttpServletResponse response){
    	HttpSession session = request.getSession();
        return "sys/login";
    }
    
    @RequestMapping(value = "/main")
    public String main (HttpServletRequest request, HttpServletResponse response){
    	HttpSession session = request.getSession();
    	//获取用户
    	SysUser user = (SysUser)session.getAttribute("user");
    	//查询用户的角色对应的菜单信息
    	sysMenuService.getMenuByUserId(user.getUserid(),request);
    	return "sys/main";
    }

    /**
     * 方法说明：获得表格数据
     * @author  ChenXH
     * @date  2018-11-29 11:14:13
     */
    @RequestMapping(value = "/toLogin")
    @ResponseBody
    public String showList (HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
    	HttpSession session = request.getSession();
    	Object loginName = params.get("loginName");
    	Object password = params.get("password");
    	
    	Example example = new Example(SysUser.class);
    	Criteria createCriteria = example.createCriteria();
    	createCriteria.andEqualTo("userid",loginName);
    	createCriteria.andEqualTo("password",password);
    	//验证账号密码
    	List<SysUser> sysUserList = sysUserService.selectByExample(example);
    	
    	if(sysUserList.size() != 0){
    		SysUser sysUser = sysUserList.get(0);
    		//获取用户的角色，根据角色获取菜单
    		getMenuListByUser(sysUser,request);
    		session.setAttribute("user", sysUser);
    		return "1";
    	}
        return "0";
    }

    /**
     * @类功能说明：获取用户的角色，根据角色获取菜单
     * @作者：Chenxh
     * @创建时间：2018年11月29日下午2:34:45
     */
	private void getMenuListByUser(SysUser sysUser, HttpServletRequest request) {
		//获取用户的角色，根据角色获取菜单
		List<SysMenu> sysMenuList = sysUserService.getMenuListByUser(sysUser);
		request.setAttribute("sysMenuList", sysMenuList);
	}
	
	/**
	 * @类功能说明：session过期或者没有登录
	 * @作者：Chenxh
	 * @创建时间：2018年12月7日下午4:53:42
	 */
	@RequestMapping("/timeOut")
	public String timeOut(){
		return "/error/timeOut";
	}

	@RequestMapping("/loginOut")
	@ResponseBody
	public void loginOut(HttpSession session){
		session.setMaxInactiveInterval(0);
	}

}
